﻿using System;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using BCGC.BLL.Helper;
using BCGC.BLL.MailManager;
using Jalal.Library.Web;
using System.Text;

public partial class Member_mailBox : MemberBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadDropDown();
            LoadData();
        }
    }

    public string GetFormattedContent(string content)
    {
        content = Server.HtmlDecode(content);
        if (content.Length > 60)
        {
            return content.Substring(0, 60);
        }
        else
        {
            return content;
        }
    }
    private void LoadDropDown()
    {
        ddlOptions.DataSource = Enum.GetValues(typeof(MailBoxOptions));
        ddlOptions.DataBind();
    }

    private void LoadData()
    {
        int totalRecords=0;
        grvInbox.DataSource = PrivateMessage.GetPrivateMessageByReceiverID(SessionManager.LoggedUser.Id, "SendDate", "Desc", 1, 10, out totalRecords);
        grvInbox.DataBind();
    }
    protected void grvInbox_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            PrivateMessage message=e.Row.DataItem as PrivateMessage;
            if (message != null)
            {
                if (message.IsRead)
                {
                    e.Row.CssClass = "MailRead";
                }
                else
                {
                    e.Row.CssClass = "MailUnread";
                }
            }
        }
    }
    protected void ddlOptions_SelectedIndexChanged(object sender, EventArgs e)
    {
        foreach (GridViewRow grv in grvInbox.Rows)
        {
            if (grv.RowType == DataControlRowType.DataRow)
            {
                CheckBox chkRead = grv.FindControl("chkRead") as CheckBox;
                HtmlInputHidden hidRead = grv.FindControl("hidRead") as HtmlInputHidden;
                 
                switch (ddlOptions.SelectedValue)
                {
                    case "Read": chkRead.Checked = Convert.ToBoolean(hidRead.Value); break;
                    case "Unread": chkRead.Checked = !Convert.ToBoolean(hidRead.Value); break;
                    case "All": chkRead.Checked = true; break;
                    case "None": chkRead.Checked = false; break;
                }
            }
          
        }
    }

    private string GetSelected()
    {
        StringBuilder str = new StringBuilder();
        foreach (GridViewRow grv in grvInbox.Rows)
        {
            if (grv.RowType == DataControlRowType.DataRow)
            {
                CheckBox chkRead = grv.FindControl("chkRead") as CheckBox;
                int messageID = Convert.ToInt32(grvInbox.DataKeys[grv.DataItemIndex]);
                if (chkRead.Checked)
                {
                    str.Append("<ID>" + messageID.ToString() + "</ID>");
                }
            }
        }

        return str.ToString();
       
    }
    protected void lnkMarkRead_Click(object sender, EventArgs e)
    {
        if (PrivateMessage.MailBoxOperations(GetSelected(), MailMessageOperations.Read))
        {
            uccResponse.SetSuccessMessage("One or More Message Marked as Read Successfully");
            LoadData();
        }
        else
        {
            uccResponse.SetFailMessage("Operation Failed.Please Try Again Later");
        }

    }
    protected void lnkMarkUnRead_Click(object sender, EventArgs e)
    {
        if (PrivateMessage.MailBoxOperations(GetSelected(), MailMessageOperations.Unread))
        {
            uccResponse.SetSuccessMessage("One or More Message Marked as UnRead Successfully");
            LoadData();
        }
        else
        {
            uccResponse.SetFailMessage("Operation Failed.Please Try Again Later");
        }

    }
    protected void lnkDelete_Click(object sender, EventArgs e)
    {
        if (PrivateMessage.MailBoxOperations(GetSelected(), MailMessageOperations.Delete))
        {
            uccResponse.SetSuccessMessage("One or More Message Deleted Successfully");
            LoadData();
        }
        else
        {
            uccResponse.SetFailMessage("Operation Failed.Please Try Again Later");
        }


    }
}
